import { Http, Headers, Response,Jsonp, RequestOptions, URLSearchParams } from '@angular/http';
import 'rxjs/add/operator/toPromise';

import { Injectable } from '@angular/core';

import { Journey } from './journey';
import { SERVER} from '../../app.config';

@Injectable()
export class JourneyService {

    constructor(private http: Http){

    }

    getJourneys(): Promise<Journey[]> {
        return this.http.get(SERVER.ORDER + "/date")
        .toPromise()
        .then(response => response.json().journey_list as Journey[])
        .catch(this.handleError);
    }

    searchJourneys(start_time: string, end_time: string): Promise<Journey[]> {
        let url = SERVER.ORDER + "/date?start_time=" + start_time + '&end_time=' + end_time;
        return this.http.get(url)
        .toPromise()
        .then(response => response.json().journey_list as Journey[])
        .catch(this.handleError);
    }

    getJourney(order_id: number) {
        return this.getJourneys()
            .then(journeys => journeys.find(journey => journey.order_id === order_id ));
    }

    private handleError(error: any): Promise<any> {
        console.error('错误：', error);

        return Promise.reject(error.message || error);
    }
}